package com.example.webjsdemo

import android.app.Service
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.IBinder
import android.util.Log
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlin.system.exitProcess


/**
 * author: tdq
 * date: #date
 * description:
 */
class ScreenService: Service() {
    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    override fun onCreate() {
        super.onCreate()
        val mScreenOnFilter = IntentFilter("android.intent.action.SCREEN_ON")
        this@ScreenService.registerReceiver(mScreenOReceiver, mScreenOnFilter)

        val mScreenOFFFilter = IntentFilter("android.intent.action.SCREEN_OFF")
        this@ScreenService.registerReceiver(mScreenOReceiver, mScreenOFFFilter)
    }

    private val mScreenOReceiver: BroadcastReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent) {
            val action = intent.action
            if (action == "android.intent.action.SCREEN_ON") {
                Log.e("123","—— SCREEN_ON ——")
                LiveEventBus.get<String>("123").post(null)
            } else if (action == "android.intent.action.SCREEN_OFF") {
                Log.e("123","—— SCREEN_OFF ——")
                LiveEventBus.get<String>("456").post(null)
                exitProcess(0)
            }
        }
    }

}